<?php
class Question extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->helper ( 'form' );
	}
	
	function questionList() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			$this->load->model ( 'Mhelp' );
			$this->load->library ( 'pagination' );
			
			$data ['num'] = $this->Mhelp->getQuestionNum ();
			$data ['page'] = $this->uri->segment ( 4, 0 );
			
			$config ['base_url'] = base_url () . '/admin/question/questionList/';
			$config ['total_rows'] = $data ['num'];
			$config ['per_page'] = PAGE_SHOW_NUM;
			$config ['uri_segment'] = 4;
			$config ['num_links'] = 2;
			$this->pagination->initialize ( $config );
			
			$data ['list'] = $this->Mhelp->getQuestionList ( $data ['page'], PAGE_SHOW_NUM );
			
			$data ['authority'] = $this->session->userdata ( 'authority' );
			$data ['adminName'] = $this->session->userdata ( 'adminName' );
			
			$this->load->view ( 'admin/header', $data );
			$this->load->view ( 'admin/nav' );
			$this->load->view ( 'admin/question_list' );
			$this->load->view ( 'admin/footer' );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有管理常见问题的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function add() {
		$this->load->model ( 'Mhelp' );
		$this->load->library ( 'pagination' );
		$this->load->helper ( 'form' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			
			$this->form_validation->set_rules ( 'question', '问题', 'trim|required|max_length[100]' );
			// $this->form_validation->set_rules ( 'answer', '回答', 'required' );
			
			if ($this->form_validation->run () == TRUE) {
				$data ['question'] = $this->input->post ( 'question' );
				$data ['answer'] = $this->input->post ( 'answer' );
				$data ['updatePerson'] = $this->session->userdata ( 'adminName' );
				$data ['updateTime'] = date ( "Y/m/d H:i:s", time () );
				$data ['id'] = $this->Mhelp->addQuestion ( $data );
				if ($data ['id']) {
					$data ['msg'] = '常见问题添加成功！';
				} else {
					$data ['msg'] = '数据插入失败！';
				}
				$data ['url'] = base_url () . 'admin/question/questionList';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['form'] = array ('id' => 'questionAddForm' );
				$data ['answer'] = $this->input->post ( 'answer' );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/question_add' );
				$this->load->view ( 'admin/footer' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有添加常见问题的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function edit() {
		$this->load->model ( 'Mhelp' );
		$this->load->library ( 'pagination' );
		$this->load->helper ( 'form' );
		
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			
			$this->form_validation->set_rules ( 'question', '问题', 'trim|required|max_length[100]' );
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$data ['item'] = $this->Mhelp->getQuestionbyId ( $data ['id'] );
			
			if ($this->form_validation->run () == TRUE) {
				$query ['question'] = $this->input->post ( 'question' );
				$query ['answer'] = $this->input->post ( 'answer' );
				$query ['updatePerson'] = $this->session->userdata ( 'adminName' );
				$query ['updateTime'] = date ( "Y/m/d H:i:s", time () );
				
				if ($this->Mhelp->updateQuestion ( $query, $data ['id'] )) {
					$data ['msg'] = '常见问题修改成功！';
				} else {
					$data ['msg'] = '数据更新失败！';
				}
				$data ['url'] = base_url () . 'admin/question/questionList';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['answer'] = $this->input->post ( 'answer' );
				$data ['form'] = array ('id' => 'questionEditForm' );
				
				$data ['authority'] = $this->session->userdata ( 'authority' );
				$data ['adminName'] = $this->session->userdata ( 'adminName' );
				
				$this->load->view ( 'admin/header', $data );
				$this->load->view ( 'admin/nav' );
				$this->load->view ( 'admin/question_edit' );
				$this->load->view ( 'admin/footer' );
			}
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有修改常见问题的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
	
	function delete() {
		if (($this->session->userdata ( 'authority' ) === '0') || ($this->session->userdata ( 'authority' ) === '1')) {
			$data ['id'] = $this->uri->segment ( 4, 0 );
			$this->load->model ( 'Mhelp' );
			
			if ($this->Mhelp->delQuestionbyId ( $data ['id'] )) {
				$data ['msg'] = '常见问题删除成功！';
			} else
				$data ['msg'] = '常见问题删除失败！';
			
			$data ['url'] = base_url () . 'admin/question/questionList';
			$this->load->view ( 'admin/admin_message', $data );
		} else {
			if ($this->session->userdata ( 'adminName' )) {
				$data ['msg'] = '您没有删除常见问题的权限！';
				$data ['url'] = base_url () . 'admin/home';
				$this->load->view ( 'admin/admin_message', $data );
			} else {
				$data ['msg'] = '请先登录！';
				$data ['url'] = base_url () . 'admin/home/login';
				$this->load->view ( 'admin/admin_message', $data );
			}
		}
	}
}
